package yns.springcloud.oakwood.guide.controller;

import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import yns.springcloud.oakwood.guide.bean.Order;
import yns.springcloud.oakwood.guide.bean.User;
import yns.springcloud.oakwood.guide.configure.Configure;

@RestController
public class ProducerController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private Configure configure;

	@Autowired
	private Registration registration;

	@GetMapping("/producer")
	public String producer(@RequestParam("value") String value) {
		String serverInfo = "ServiceId : " + registration.getServiceId() + " Host : " + registration.getHost()
				+ " Port : " + registration.getPort();

		logger.info(configure.getDefaultZone());
		logger.info(serverInfo);
		logger.info("Request value : " + value);

		String response = "Producer : " + serverInfo + " -- Request value : " + value;

		return response;
	}

	@PostMapping("/producer2")
	public Order producer2(@RequestBody User user) {
		String serverInfo = "ServiceId : " + registration.getServiceId() + " Host : " + registration.getHost()
				+ " Port : " + registration.getPort();

		logger.info(serverInfo);
		logger.info("Request user : " + user.toString());
		
		String desc = "Producer : Port : " + configure.getPort() + " -- Request user : " + user.toString();

		Order order = new Order();
		order.setOrderId(LocalDateTime.now().toString());
		order.setDesc(desc);

		return order;
	}

	@PutMapping("/producer3")
	public Order producer3(@RequestBody User user) {
		String serverInfo = "ServiceId : " + registration.getServiceId() + " Host : " + registration.getHost()
				+ " Port : " + registration.getPort();

		logger.info(serverInfo);
		logger.info("Request user : " + user.toString());
		
		String desc = "Producer : Port : " + configure.getPort() + " -- Request user : " + user.toString();

		Order order = new Order();
		order.setOrderId(LocalDateTime.now().toString());
		order.setDesc(desc);

		return order;
	}
}
